*Role of LLM Tightness

clear 

use "${output_stata}\main_regression.dta", clear // Load main regression data

bys sirtg year code_entry: gen id = _n // Generate unique id within groups
drop if id > 1 // Keep only the first observation per group

merge 1:m sirtg year code_entry using "${input_stata}\llm_tightness_agg" // Merge with tightness data
keep if _merge == 3 
drop if tightness_mean == . 

*****************************************************************************
* INTERACTIONS TIGHT LABOR MARKETS
*****************************************************************************
keep if share > 0.01 & share < . // Keep observations with share >0.01 (same as main reg)

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ , absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num) 

egen q_tight = xtile(tightness_mean) if e(sample), nq(3) // Create terciles of tightness_mean
drop if q_tight == . // Drop observations with missing tercile

* Create dummy variables for each tercile
gen q_tight1 = (q_tight == 1)
gen q_tight2 = (q_tight == 2)
gen q_tight3 = (q_tight == 3)

* Interaction terms with HC_distance_s_brut_t1
gen q_tight1_HC = (q_tight == 1) * HC_distance_s_brut_t1
gen q_tight2_HC = (q_tight == 2) * HC_distance_s_brut_t1
gen q_tight3_HC = (q_tight == 3) * HC_distance_s_brut_t1

* Variable labels
label variable q_tight2 "2nd tercile of Labor scarcity$\_{\textit{g,n,t-1}}$" 
label variable q_tight3 "3rd tercile of Labor scarcity$\_{\textit{g,n,t-1}}$"

label variable q_tight2_HC "2nd t. Lab. scarc.$\_{\textit{g,n,t-1}}\times$ HC Distance$\_{\textit{g,n,t-1}}$" 
label variable q_tight3_HC "3rd t. Lab. scarc.$\_{\textit{g,n,t-1}}\times$ HC Distance$\_{\textit{g,n,t-1}}$"  
egen bassin_year = group(codcom_root year) // Create group variable for codcom_root and year

* Regressions for each tercile 
reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if q_tight == 1 /*
*/ , absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
estimates store tight_1
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"
estadd local code_bassin "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if q_tight == 2 /*
*/ , absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
estimates store tight_2
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"
estadd local code_bassin "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if q_tight == 3 /*
*/ , absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
estimates store tight_3
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"
estadd local code_bassin "Yes"

egen double gp1 = group(orig_dest_year q_tight) // Create group variable
egen double gp2 = group(codcom_root q_tight) // Create group variable

* Regression with interactions and fixed effects
reghdfe buy i.q_tight##c.HC_distance_s_brut_t1 /*
*/ i.q_tight##c.vaj_eff_t1 i.q_tight##c.diversity_t1 i.q_tight##c.size_t1 i.q_tight##c.immo_eff_t1 i.q_tight##c.sal_eff_t1 i.q_tight##c.tresact_eff_t1 /*
*/ , absorb(gp1 gp2) cluster(apgr_1_num code_entry_num codcom_root)

* Table export using esttab
esttab tight_1 tight_2 tight_3 using "${export}\tightness.tex", /*
*/ keep(HC_distance*) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/ alignment(D{.}{.}{-1}) /*     
*/ title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/ collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest_year code_bassin r2_a N, fmt(0 0 0 3 %15.0fc) /*
*/ labels(`"Controls"' `"Sector of Origin $\times$ Entry $\times$ Year FE"' `"Local labor market FE"' `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/ layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))

* MAP tightness
* Regression with residuals calculation
reghdfe buy  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 1 /*
*/ , resid absorb(orig_dest_year codcom_root ) cluster(apgr_1_num code_entry_num codcom_root)
predict buy_hat, resid
su buy if e(sample)
replace buy_hat = buy_hat + `r(mean)'
su buy_hat if e(sample)

reghdfe HC_distance_s_brut_t1  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 1 /*
*/ , resid absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
predict HC_distance_hat, resid
su HC_distance_s_brut_t1 if e(sample)
replace HC_distance_hat = HC_distance_hat + `r(mean)'
su HC_distance_hat if e(sample)

reghdfe buy  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 2 /*
*/ , resid absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
predict buy_hat2, resid
su buy if e(sample)
replace buy_hat2 = buy_hat2 + `r(mean)'
su buy_hat2 if e(sample)
replace buy_hat = buy_hat2 if e(sample)
drop buy_hat2

reghdfe HC_distance_s_brut_t1  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 2 /*
*/ , resid absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
predict HC_distance_hat2, resid
su HC_distance_s_brut_t1 if e(sample)
replace HC_distance_hat2 = HC_distance_hat2 + `r(mean)'
su HC_distance_hat2 if e(sample)
replace HC_distance_hat = HC_distance_hat2 if e(sample)
drop HC_distance_hat2

reghdfe buy  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 3 /*
*/ , resid absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
predict buy_hat2, resid
su buy if e(sample)
replace buy_hat2 = buy_hat2 + `r(mean)'
su buy_hat2 if e(sample)
replace buy_hat = buy_hat2 if e(sample)
drop buy_hat2

reghdfe HC_distance_s_brut_t1  /*
*/ vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/  if q_tight == 3 /*
*/ , resid absorb(orig_dest_year codcom_root) cluster(apgr_1_num code_entry_num codcom_root)
predict HC_distance_hat2, resid
su HC_distance_s_brut_t1 if e(sample)
replace HC_distance_hat2 = HC_distance_hat2 + `r(mean)'
su HC_distance_hat2 if e(sample)
replace HC_distance_hat = HC_distance_hat2 if e(sample)
drop HC_distance_hat2

replace HC_distance_hat = 1 if HC_distance_hat>1

* Binned scatter plot
binscatter buy_hat HC_distance_hat, /*
*/ by(q_tight) nquantiles(15) xtitle("HC Distance", height(5)) ytitle("1(Buy)", height(5)) /*
*/ legend(order(1 "1st tercile of Tightness" 2 "2nd tercile of Tightness" 3 "3rd tercile of Tightness") off) /*
*/ text(0.018 0.47 "Low tightness:" "b:0.001 (SD:0.020)", size(small) color(ebblue)) /*
*/ text(0.040 0.47 "Medium tightness:" "b:-0.013 (SD:0.022)", size(small) color(cranberry))  /*
*/ text(0.027 0.47 "High tightness:" "b:0.023** (SD:0.011)", size(small) color(dkgreen)) color(ebblue cranberry dkgreen) /*
*/ xlabel(0.40(0.1)1) xscale(range(0.40 1)) 

graph export "${export}\tightness.pdf", as(pdf) replace 
